package com.hourse.app.utils;

import com.hourse.app.vo.web.PlatformSession;

import java.util.*;

/**
 * @使用描述: CurrentThread
 * @作 者: rain
 * @创建时间: 2020-06-25 09:36
 */
public class CurrentThread {


  private final static ThreadLocal<HashMap<String, Object>> valueMap = new ThreadLocal<>();

  public static void put(String key, Object value) {
    if (valueMap.get() == null) {
      valueMap.set(new HashMap<>());
    }
    valueMap.get().put(key, value);
  }

  public static Object get(String key) {

    return valueMap.get().get(key);
  }

  public static void remove(String key) {

    valueMap.get().remove(key);
  }

  public static void setUserId(String userId) {
    valueMap.get().put("_userId", userId);
  }

  public static String getUserId() {
    return
      valueMap.get() != null
        && valueMap.get().
        containsKey("_userId") ? (String) valueMap.get().get("_userId") : "";
  }

  public static void setToken(String userId) {
    valueMap.get().put("token", userId);
  }

  public static String getToken() {
    return (String) valueMap.get().get("token");
  }

  /* public static void setRoleGroupId(String userId,String roleGroupId){
     valueMap.get().put(userId + "_roleGroupId",roleGroupId);
   }

   public static String getRoleGroupId(){
     return (String)valueMap.get().get(getUserId() + "_roleGroupId");
   }
 */
  public static boolean isAdmin() {

    if (valueMap.get().get("_admin") == null) {

      return false;
    }

    return (boolean) valueMap.get().get("_admin");
  }

  public static void setAdminStatus(boolean status) {
    valueMap.get().put("_admin", status);
  }

  public static void setRoles(Set<String> roles) {
    valueMap.get().put("_roles", roles);
  }

  public static Set<String> getRoles() {

    if (valueMap.get() == null) {
      return new HashSet<>();
    }
    return (Set<String>) valueMap.get().get("_roles");
  }

  public static void setCurrentRoleId(String roleGroupId) {
    valueMap.get().put("role_id", roleGroupId);
  }

  public static String getCurrentRoleId() {
    return (String) valueMap.get().get("role_id");
  }

  public static void setCurrentEnterPriseId(String eid) {
    valueMap.get().put("curt_eid", eid);
  }

  public static String getCurrentEnterPriseId() {
    return (String) valueMap.get().get("curt_eid");
  }

  public static void setTimer(Timer timer, String userName) {
    valueMap.get().put(userName + "sockerCode", timer);
  }

  public static Timer getTimer(String userName) {
    return (Timer) valueMap.get().get(userName + "sockerCode");
  }

  public static void setPlatform(Map<String, PlatformSession> list) {
    valueMap.get().put("platforms", list);
  }

  public static Map<String, PlatformSession> getPlatform() {
    return (Map<String, PlatformSession>) valueMap.get().get("platforms");
  }

  public static String getUKey(String platfrom) {
    Map<String, PlatformSession> map = (Map<String, PlatformSession>) valueMap.get().get("platforms");
    return map.get(platfrom).getKey();
  }

  public static String getuSecret(String platfrom) {
    Map<String, PlatformSession> map = (Map<String, PlatformSession>) valueMap.get().get("platforms");
    return map.get(platfrom).getuSecret();
  }
}
